import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../pages/Login'
import Home from '../pages/Home'
import Welcome from '../pages/Welcome'
import Users from '../pages/Users'
import Rights from '../pages/power/Rights'
import Roles from '../pages/power/Roles'
import Categories from '../pages/goods/Categories'
import Params from '../pages/goods/Params'
import GoodsList from '../pages/goods/List'
import Add from '../pages/goods/Add'
import Order from '../pages/order/Order'
import Report from '../pages/report/Report'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    {
      path: '/login',
      component: Login,
      meta: { title: '登录' },
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome,
          meta: { title: '欢迎' }
        }, {
          path: '/users',
          component: Users,
          meta: { title: '用户管理' }
        },
        {
          path: '/rights',
          component: Rights,
          meta: { title: '权限列表' }
        },
        {
          path: '/roles',
          component: Roles,
          meta: { title: '角色列表' }
        }, {
          path: '/categories',
          component: Categories,
          meta: { title: '商品分类' }
        }, {
          path: '/params',
          component: Params,
          meta: { title: '分类参数' }
        },
        {
          path: '/goods',
          component: GoodsList,
          meta: { title: '商品列表' }
        },
        {
          path: '/goods/add',
          component: Add,
          meta: { title: '添加商品' }
        },
        {
          path: '/orders',
          component: Order,
          meta: { title: '订单列表' }
        },
        {
          path: '/reports',
          component: Report,
          meta: { title: '数据报表' }
        }
      ]
    }]
})

// 挂载全局前置路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')// 强制跳转
  next()
})
// 挂载全局后置路由导航守卫
router.afterEach((to, from) => {
  if (to.meta.title) {
    document.title = to.meta.title // 修改网页的title
  } else {
    document.title = '登录'
  }
})

export default router
